#ifndef READER_H
#define READER_H

#include "binary_chunk.h"
#include "err.h"

#define BUF_SIZE 512

/* ------------------------------------------- */

/*
 * forward declaration
 */

typedef struct Reader reader_t;

/* ------------------------------------------- */

/*
 * structure
 */

struct Reader {
    byte *data;         /* point to the binary chunk data */
    size_t bufsz;
};

/* ------------------------------------------- */

/*
 * function declaration
 */

byte readByte(reader_t *reader);        /* read 1 byte */
word readWord(reader_t *reader);        /* read 4 bytes */
dword readDword(reader_t *reader);      /* read 8 bytes */
int64 readLuaInt(reader_t *reader);     /* read 8 bytes */
float64 readLuaNum(reader_t *reader);   /* read 8 bytes */
byte *readString(reader_t *reader);
byte *readBytes(reader_t *reader, size_t n);
void checkHeader(reader_t *reader);
void readProto(reader_t *reader, byte *parent_srcname, proto_t *proto);
word *readInstr(reader_t *reader);
void *readConst(reader_t *reader);

/* ------------------------------------------- */

#endif  /* READER_H */